//
//  CommonViewController.m
//  第三组_阅读器
//
//  Created by My Mac on 2016/12/25.
//  Copyright © 2016年 My Mac. All rights reserved.
//

#import "CommonViewController.h"
#import "CommonCell.h"
#import "ReadViewController.h"
@interface CommonViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) NSMutableArray * data;
@property(nonatomic,strong) NSArray * arrData;

@end

@implementation CommonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    [self setData];
}
-(void)setData
{
    _data = [NSMutableArray new];
    for (int i=1; i<=18; i++) {
        [_data addObject:[NSString stringWithFormat:@"%02d",i]];
    }
    _arrData = @[@"爱在仙境的日子",@"那时长大",@"不必知道我是谁",@"当你孤单你会想起谁",@"蝴蝶来过这世界",@"ID",@"沙漏",@"十年",@"糖衣",@"调频",@"我不是你的冤家",@"我要我们在一起",@"校园里的裙摆",@"小妖",@"最熟悉的陌生人",@"左耳",@"左耳",@"未知"];
}
-(void)initTableView
{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * ID = @"cell";
    CommonCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell==nil){
        cell = [[CommonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.bookName.text = _arrData[indexPath.row];
    cell.author.text = @"作者:饶雪漫";
    cell.imgView.image = [UIImage imageNamed:_data[indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadViewController * readView = [[ReadViewController alloc]init];
    readView.navigationItem.title = _arrData[indexPath.row];
    NSString * fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",indexPath.row+1] ofType:@"txt"];
    readView.bookStr = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:readView];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
